<style>
  .customerReward .el-input.is-disabled .el-input__inner{
    color: black;
  }
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户帐号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户帐号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动" prop="activityNo">
        <customSelect
          v-model="selectQueryParams.subject"
          :dataList="activityOptions"
          labelKey="subject"
          valueKey="no"
          v-on:selectPageChange='getActivityOptions'
          v-on:selectChange="updateActivityOptions"
          :pageInfo="selectQueryParams"
          @click="getActivityOptions"
        />
      </el-form-item>
      <el-form-item label="奖品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入奖品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:reward:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rewardList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户帐号" align="center" prop="username" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="活动名称" align="center" prop="activityName" />
      <el-table-column label="奖品名称" align="center" prop="productName" />
      <el-table-column label="奖品数量" align="center" prop="productCount" />
      <el-table-column label="获奖日期" align="center" prop="rewardDate" :formatter="dateFmt" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.rewardDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['customer:reward:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户奖励记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="记录编号" prop="logNo">
          <el-input v-model="form.logNo" placeholder="请输入记录编号" />
        </el-form-item>
        <el-form-item label="用户编号" prop="customerNo">
          <el-input v-model="form.customerNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="活动编号" prop="activityNo">
          <el-input v-model="form.activityNo" placeholder="请输入活动编号" />
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="奖品编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入奖品编号" />
        </el-form-item>
        <el-form-item label="奖品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入奖品名称" />
        </el-form-item>
        <el-form-item label="奖品数量" prop="productCount">
          <el-input v-model="form.productCount" placeholder="请输入奖品数量" />
        </el-form-item>
        <el-form-item label="获奖日期" prop="rewardDate">
          <el-input v-model="form.rewardDate" placeholder="请输入获奖日期" />
        </el-form-item>
        <el-form-item label="获奖时间" prop="rewardTime">
          <el-input v-model="form.rewardTime" placeholder="请输入获奖时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form class="customerReward" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="记录编号" prop="logNo">
          <el-input disabled v-model="form.logNo" placeholder="请输入记录编号" />
        </el-form-item>
        <el-form-item label="用户编号" prop="customerNo">
          <el-input disabled v-model="form.customerNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="用户帐号" prop="username">
          <el-input disabled v-model="form.username" placeholder="请输入用户帐号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input disabled v-model="form.nickname" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="活动编号" prop="activityNo">
          <el-input disabled v-model="form.activityNo" placeholder="请输入活动编号" />
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input disabled v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="奖品编号" prop="productNo">
          <el-input disabled v-model="form.productNo" placeholder="请输入奖品编号" />
        </el-form-item>
        <el-form-item label="奖品名称" prop="productName">
          <el-input disabled v-model="form.productName" placeholder="请输入奖品名称" />
        </el-form-item>
        <el-form-item label="奖品数量" prop="productCount">
          <el-input disabled v-model="form.productCount" placeholder="请输入奖品数量" />
        </el-form-item>
        <el-form-item label="获奖日期" prop="rewardDate">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.rewardDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择获奖日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReward, getReward, delReward, addReward, updateReward, exportReward, listRewardVo, getRewardVoByLogNo } from "@/api/customer/reward";
import { listActivityAllOptions } from "@/api/activity/info";
import CustomSelect from '@/components/Select';

export default {
  name: "Reward",
  components: {
    CustomSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户奖励记录表格数据
      rewardList: [],
      // 活动集合
      activityOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logNo: null,
        customerNo: null,
        customerName: null,
        activityNo: null,
        activityName: null,
        productNo: null,
        productName: null,
        productCount: null,
        rewardDate: null,
        rewardTime: null
      },
      // 查询参数
      selectQueryParams: {
        pageNum: 1,
        pageSize: 10,
        subject: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户奖励记录列表 */
    getList() {
      this.loading = true;
      listRewardVo(this.queryParams).then(response => {
        this.rewardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        logNo: null,
        customerNo: null,
        customerName: null,
        activityNo: null,
        activityName: null,
        productNo: null,
        productName: null,
        productCount: null,
        rewardDate: null,
        rewardTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.selectQueryParams.subject = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户奖励记录";
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const logNo = row.logNo
      getRewardVoByLogNo(logNo).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "用户奖励记录详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const logNo = row.logNo
      getRewardVoByLogNo(logNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户奖励记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReward(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReward(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户奖励记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReward(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户奖励记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReward(queryParams);
        }).catch(() => {});
    },
    /** 日期格式化 */
    dateFmt(str) {
      if(!str){
        return "";
      }
      return str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8);
    },
    updateActivityOptions(val){
      var valArr = val.split("-");
      this.queryParams.activityNo = valArr[0];
      this.selectQueryParams.subject = valArr[1];
    },
    getActivityOptions(pageNum,pageSize,queryContent){
      if(!pageNum){
        pageNum = this.selectQueryParams.pageSize;
      }
      if(!pageSize){
        pageSize = this.selectQueryParams.pageSize;
      }
      this.selectQueryParams.pageNum = pageNum;
      this.selectQueryParams.pageSize = pageSize;
      this.selectQueryParams.subject = queryContent;

      listActivityAllOptions(this.selectQueryParams).then(response => {
        this.activityOptions = response.rows;
        this.selectQueryParams.total = response.total;
      });
    }
  }
};
</script>
